const app = getApp()
Page({
  data: {
    notice:'',//提示语
    loadingHidden: true,
    isshow:false,
    hasUserInfo:false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    disabled: false, //按钮是否禁用
    studentid: '', //获取到的手机栏中的值
    code: '',
    NewChanges: '',
    NewChangesAgain: '',
    success: false,
    state: '',
    school_id:'',//当前选择的学校的id,
    schoolname:'',
    phone:'',
    link:'',
    studentdata:false,
    user_id:'',//选择的学生id
    user_name:'',
    user_type:'',
  },
  onLoad: function () {
    var self=this;
    if (app.globalData.bindInfo) {
      wx.navigateTo({
        url: '../index/index',
      })
    }else{
      self.getnotice();
      self.setData({
        isshow: true
      })
    }
    if (app.globalData.userInfo) {
      self.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (self.data.canIUse) {
      app.userInfoReadyCallback = res => {
        self.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      //已经授权。可以直接获取信息
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          self.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    self.setData({
      loadingHidden: true
    })
  },
  onShow: function () {
    var that = this;
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];
    if (currPage.data.link) {
      that.setData({ 
        school_id: currPage.data.id,
        schoolname: currPage.data.schoolname,
        link: currPage.data.link,
        });
    }
    if (currPage.data.user_id) {
      that.setData({
        user_id: currPage.data.user_id,
        user_name: currPage.data.user_name,
        class_text: currPage.data.class_text,
        user_type: currPage.data.user_type,
      });
    }
  },
  //选择学校
  selectschool: function (e) {
    var school_id = this.data.school_id;
    var schoolname = this.data.schoolname;
    wx.navigateTo({
      url: '../binding/selectschool?school_id=' + school_id + '&schoolname=' + schoolname
    })
  },
//获取输入的学生编号
  handleInputStudentid: function (e) {
    this.setData({
      studentid: e.detail.value
    })
  },
  //获取手机号
  getphone: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },

//获取邀请码
  handleVerificationCode: function (e) {
    this.setData({
      code: e.detail.value
    })
  },
  handleNewChanges: function (e) {
    this.setData({
      NewChanges: e.detail.value
    })
  },
  getaccountinfo: function (user_id, sign,link){
    var sip = 'http://' + link+'/';
    var foot = 'rest?method=base.user.getUserInfo&uid=' + user_id + '&sign=' + sign;
    if (user_id && sign) {
        wx.request({
          url: getApp().globalData.baseUrl + 'test1',
          method: "GET",
          data: {
            sip: sip,
            foot: foot,
          },
          header: {
            "content-type": "application/x-www-form-urlencoded"
          },
          success: function (res) {
            app.globalData.accountInfo = res.data.data.data;
          }
        })
      }
    },
  upcode: function (code, oauth, openid,link) {
    wx.request({
      url: getApp().globalData.baseUrl + 'upcode',
      method: "POST",
      data: {
        code: code,
        oauth: oauth,
        openid: openid,
        link:link
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        if (res.data.code==0){
          wx.showToast({
            title:'绑定错误',
            icon: 'none',
            duration: 2000
          })
          wx.redirectTo({
            url: '../binding/binding'
          })
        }
      }
    })
  },
  //刷新
  shuaxin: function (e) {
    console.log(app.globalData.bindInfo);
    if (app.globalData.bindInfo) {
      wx.navigateTo({
        url: '../index/index',
      })
    } else {
      wx.redirectTo({
        url: '../binding/binding'
      })
    }
  },
//下一步
  nextsubmit: function (e) {
    var that = this;
    var school_id = that.data.school_id;
    var phone = that.data.phone;
    var link = that.data.link;
    
    var oauth = "mapp";
    var openid = getApp().globalData.openid;
    if (school_id == '') {
      wx.showToast({
        title: '请选择学校',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (phone == '') {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (!(/^1[34578]\d{9}$/.test(phone))) {
      wx.showToast({
        title: '手机号格式错误',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (link == '') {
      wx.showToast({
        title: '重新选择学校',
        icon: 'none',
        duration: 2000
      })
      return
    }
    wx.navigateTo({
      url: '../binding/studentdata?phone=' + phone + '&link=' + link
    })
  },
  //立即绑定接口
  newsubmit: function (e) {
    var that = this;
    var mobile = that.data.phone;
    var oauth = "mapp";
    var openid = getApp().globalData.openid;
    var user_id = that.data.user_id;
    var link = that.data.link;
    // mobile = 15067097276;
    if (mobile == '') {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        duration: 2000
      })
      return
    }
   if (user_id == '') {
      wx.showToast({
        title: '请选择学生',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (link == '') {
      wx.showToast({
        title: '请选择学校',
        icon: 'none',
        duration: 2000
      })
      return
    }
    wx.request({
      url: getApp().globalData.baseUrl + 'checkBindMobile',
      method: "POST",
      data: {
        mobile: mobile,
        openid: openid,
        oauth: oauth,
        user_id: user_id,
        link: link
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        if (res.data.code == 200) {
          if (res.data.data.code == 1) {
            if (res.data.data.data.user_type == 1) {
              that.getaccountinfo(res.data.data.data.user_id, res.data.data.data.sign, res.data.link);//教师
            }
            app.globalData.bindInfo = res.data.data.data;
            app.globalData.isbind = true;
            app.globalData.schlink = res.data.link;
            wx.showToast({
              title: '绑定成功',
              icon: 'succes',
              duration: 1500,
              mask: true
            })
            wx.switchTab({
              url: '../index/index',
            })
          } else {
            wx.showToast({
              title: res.data.data.msg,
              icon: 'none',
              duration: 1500
            })
          }
        } else {
          var titleword = "绑定失败!";
          wx.showToast({
            title: titleword,
            icon: 'none',
            duration: 1500
          })
        }
      }
    })
  },

  submit: function (e) {
    var that = this;
    var code = that.data.code;
    var oauth="mapp";
    var openid = getApp().globalData.openid;
  if (code == '') {
      wx.showToast({
        title: '请输入邀请码',
        icon: 'none',
        duration: 2000
      })
      return
    }
      wx.request({
        url: getApp().globalData.baseUrl + 'binduser',
        method: "POST",
        data: {
          sign: code,
          openid: openid
        },
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          if (res.data.code==200){
            if (res.data.data.code==1){
              if (res.data.data.data.user_type == 1){
                that.getaccountinfo(res.data.data.data.user_id, res.data.data.data.sign,res.data.link);//教师
              }
              app.globalData.bindInfo = res.data.data.data;
              app.globalData.isbind = true;
              app.globalData.schlink = res.data.link;
              wx.showToast({
                title: '绑定成功',
                icon: 'succes',
                duration: 1500,
                mask: true
              })
              wx.switchTab({
                url: '../index/index',
              })
              that.upcode(code, oauth, openid, res.data.link);//绑定之后更改邀请码
            }else{
              wx.showToast({
                title: '验证码错误!',
                icon: 'none',
                duration: 1500
              })
            }
          }else{
            wx.showToast({
              title: '验证码错误!',
              icon: 'none',
              duration: 1500
            })
          }
        }
      })
  },

  onShareAppMessage: function () {
  },
  //授权获取用户信息 授权之后存入缓存且出现绑定页
  getUserInfo: function (e) {
    var that=this;
    if (e.detail.userInfo){
      app.globalData.userInfo = e.detail.userInfo;
      that.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true,
        canIUse:false
      })
    }
  },
  //获取提示语
   getnotice: function () {
    var self = this;
    wx.request({
      url: getApp().globalData.baseUrl + 'getnotice',
      method: "GET",
      data: {
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        if (res.data){
          self.setData({
            notice: res.data
          })
        }
      }
    })
  },

})